package org.apache.sad.core.framework.utils;

import java.text.MessageFormat;

/**
 * Provides convenient methods as regards to obfuscation.
 */
public class ObfuscationUtil {
    private static String PATTERN = "(<(\\w*:)?{0}(\\s.*?)?>).*?(</(\\2?){0}>)";
    private static String REPLACEMENT_STRING = "$1********$4";

    public static String obfuscateXmlString(String xmlString, String[] obfuscatedFields) {
    	if ( xmlString == null || obfuscatedFields == null || obfuscatedFields.length == 0 ) {
    		return xmlString;
    	}

    	String result = xmlString;
    	for (String fieldName : obfuscatedFields)
    	{
    		String regex = MessageFormat.format(PATTERN, new Object [] { fieldName });
    		result = result.replaceAll(regex, REPLACEMENT_STRING);
    	}
    	return result;
    }

    public static String obfuscateXmlString(String xmlString) {
    	return obfuscateXmlString(xmlString, ObfuscationConfiguration.getObfuscatedFieldsAsArray());
    }
}
